package com.pda.xinma.ui.demonstrate;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.xinma.R;
import com.pda.xinma.bean.ConnectionBean;
import com.pda.xinma.bean.ScanBean;
import com.pda.xinma.bean.ScanResultBean;
import com.pda.xinma.bean.request.AgvMoveRequest;
import com.pda.xinma.contract.DemonstrateContract;
import com.pda.xinma.dialog.AffirmDialog;
import com.pda.xinma.presenter.DemonstratePresenter;
import com.pda.xinma.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
@Route(path = AppActivityKey.DEMONSTRATEACTIVITY)
public class DemonstrateActivity extends BaseMvpActivity<DemonstratePresenter> implements DemonstrateContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_stop)
    TextView tvStop;

    @Override
    public DemonstratePresenter getPresenter() {
        return new DemonstratePresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_demomstrate;
    }

    @Override
    public void initView() {
        presenter.showTaskCycle();
    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                presenter.showTaskCycle();
                if (!TextUtils.isEmpty(scanBean.getCode())) {
                    presenter.scanTheQRCodeToVerify(scanBean.getCode());
                }
            }
        });
    }

    @Override
    public void getData() {

    }
    @Override
    public void setScanBean(ScanResultBean scanBean) {
        if (scanBean.getType().equals("KW")) {
            tvStorageBin.setText(scanBean.getCode());
        } else /*if ()*/ {
            ToastUtils.showToast(mContext, "请扫接驳工位？");
        }
    }
    private String cycle;
    @Override
    public void setCyle(String cycle) {
        this.cycle=cycle;
        if (cycle.equals("false")){
            tvStop.setText("开始");
            tvStop.setBackgroundResource(R.drawable.bg_69d21e_4);
        }else {
            tvStop.setText("停止");
            tvStop.setBackgroundResource(R.drawable.bg__53946_4);
        }
    }

    @Override
    public void onSuccess(String msg) {
        if (!TextUtils.isEmpty(msg)){
            showMessage(msg);
            tvStorageBin.setText("");
        }
        presenter.showTaskCycle();
    }

    @OnClick({R.id.tv_start, R.id.tv_stop})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_start:
                if (TextUtils.isEmpty(tvStorageBin.getText().toString())){
                    ToastUtils.showToast(mContext, "请扫接驳工位？");
                    return;
                }
                AffirmDialog dialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        AgvMoveRequest connectionBean=new AgvMoveRequest();
                        connectionBean.setConnectionCode(tvStorageBin.getText().toString());
                        presenter.confirmCarry(connectionBean);
                    }
                });
                dialog.show("确定下发吗");
                break;
            case R.id.tv_stop:
                String content="";
                String title="";
                if (cycle.equals("false")){
                    content="true";
                    title="确定要开始吗？";
                }else {
                    content="false";
                    title="确定要停止吗？";
                }
                String finalContent = content;
                AffirmDialog dialog1=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        presenter.countBackConfirm(finalContent);
                    }
                });
                dialog1.show(title);
                break;
        }
    }
}
